package com.zoer.presentation.csasc;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Expression;

import com.zoer.commons.presentation.BaseAction;
import com.zoer.commons.service.PaginationServiceImpl;
import com.zoer.domain.DiseaseRecipe;
import com.zoer.domain.view.ViewRecipeDisease;
import com.zoer.domain.view.SearchResult;
import com.zoer.domain.view.ViewRecipeDetail;
import com.zoer.hibdao.DiseaseRecipeDAO;
import com.zoer.hibdao.RecipeDetailDAO;
public class RecipeAction extends BaseAction {
	private static final long serialVersionUID = 1L;
	private SearchResult<ViewRecipeDetail> recipeDetail=new SearchResult<ViewRecipeDetail>();
	private SearchResult<ViewRecipeDisease> diseaseRecipe=new SearchResult<ViewRecipeDisease>();
	private int pageNum = 1;
	public int getPageNum() {
		return pageNum;
	}
	public void setPageNum(int pageNum) {
		this.pageNum = pageNum;
	}
	@SuppressWarnings("unchecked")
	public String list(){
		HttpServletRequest request = ServletActionContext.getRequest();
		Criterion c1 = null;
		String diseaseid = request.getParameter("diseaseid");
		if (diseaseid != null && diseaseid.length() > 0) {
			c1 = Expression.eq("diseaseid",diseaseid);
		}
		getRecipePagination(c1);
		
		request.setAttribute("diseaseRecipes",diseaseRecipe.getStatisticsList());
		return "list";
	}
	@SuppressWarnings("unchecked")
	public void queryRecipes() throws Exception{
		HttpServletRequest request = ServletActionContext.getRequest();
		Criterion c1 = null;
		String diseaseid = request.getParameter("diseaseid");
		if (diseaseid != null && diseaseid.length() > 0) {
			c1 = Expression.eq("diseaseid",diseaseid);
		}
		getRecipePagination(c1);
		JSONObject jsonObject = JSONObject.fromObject(diseaseRecipe);
		HttpServletResponse res = ServletActionContext.getResponse();
		res.setCharacterEncoding("UTF-8");
		res.getWriter().print(jsonObject.toString());
	}
	@SuppressWarnings("unchecked")
	private void getRecipePagination(Criterion... criterion) {
		PaginationServiceImpl<ViewRecipeDisease> ps = (PaginationServiceImpl<ViewRecipeDisease>) getBean("pageImplService");
		ps.setPo(ViewRecipeDisease.class);
		ps.setPageNum(pageNum);
		diseaseRecipe.setPaginationBar(ps.getPaginationBar(criterion));
		diseaseRecipe.setStatisticsList(ps.getByCriterion(criterion));
	}
	public void deleteRecipe()
	{
//		try {
//			DiseaseRecipeDAO drdao = (DiseaseRecipeDAO) getBean("diseaseRecipeDao");
//			HttpServletResponse res = ServletActionContext.getResponse();
//			HttpServletRequest request = ServletActionContext.getRequest();
//			String result = "1";
//			String id = request.getParameter("id");
//			String recipeid = request.getParameter("recipeid");
//			if(id!=null&&!id.isEmpty())
//			{
//				DiseaseRecipe recipe=new DiseaseRecipe();
//				recipe.setRecipeid(recipeid);
//				recipe.setId(id);
//				result=drdao.deleteRecipe(recipe);
//			}
//			res.setCharacterEncoding("UTF-8");
//			res.getWriter().print(result);
//		} catch (IOException e) {
//			e.printStackTrace();
//		}
	}
	@SuppressWarnings("unchecked")
	public String detailList() throws Exception
	{
		HttpServletRequest request = ServletActionContext.getRequest();
		Criterion c1 = null;
		String recipeid = request.getParameter("recipeid");
		if (recipeid != null && recipeid.length() > 0) {
			c1 = Expression.eq("recipeid",recipeid);
		}
		getRecipeDetailPagination(c1);
		request.setAttribute("recipedetails",recipeDetail.getStatisticsList());
		return "detailList";
	}
	@SuppressWarnings("unchecked")
	public void queryRecipeDetails() throws Exception{
		HttpServletRequest request = ServletActionContext.getRequest();
		Criterion c1 = null;
		String recipeid = request.getParameter("recipeid");
		if (recipeid != null && recipeid.length() > 0) {
			c1 = Expression.eq("recipeid",recipeid);
		}
		getRecipeDetailPagination(c1);
		JSONObject jsonObject = JSONObject.fromObject(recipeDetail);
		HttpServletResponse res = ServletActionContext.getResponse();
		res.setCharacterEncoding("UTF-8");
		res.getWriter().print(jsonObject.toString());
	}
	@SuppressWarnings("unchecked")
	private void getRecipeDetailPagination(Criterion... criterion) throws Exception {
		PaginationServiceImpl<ViewRecipeDetail> ps = (PaginationServiceImpl<ViewRecipeDetail>) getBean("pageImplService");
		ps.setPo(ViewRecipeDetail.class);
		ps.setPageNum(pageNum);
		recipeDetail.setPaginationBar(ps.getPaginationBar(criterion));
		recipeDetail.setStatisticsList(ps.getByCriterion(criterion));
	}
	public void deleteRecipeDetails()
	{
		try {
			RecipeDetailDAO dao = (RecipeDetailDAO) getBean("recipeDetailDao");
			HttpServletResponse res = ServletActionContext.getResponse();
			HttpServletRequest request = ServletActionContext.getRequest();
			String result = "1";
			String[] detailIds = request.getParameter("ids").split("\\|");
			if (detailIds != null) {
				result =dao.deleteRecipeDetails(detailIds);
			}
			res.setCharacterEncoding("UTF-8");
			res.getWriter().print(result);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
